/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkTcpSession.h"
#include "MkTcpServer.h"
//#include "MkNetworkUtil.h"
#include "MkEvent/MkEvent.h"
#include "MkUtil/MkUtil.h"
#include "MkUtil/MkBuffer.h"
#include "MkUtil/MkLog.h"


#define MaxBufferSize   10
#define BufferPacketLen MtuSize*10
CMkTcpSession::CMkTcpSession(CMkEvent* pEvent, CMkTimer* pTimer, CMkTcpServer* pServer)
    : CMkSocket(CMkSocket::TcpSession, pEvent, pTimer)
    , m_pTcpServer(pServer)
{
}

Uint32 CMkTcpSession::OnEventRemoved()
{
    CMkSocket::OnEventRemoved();
    if (m_pTcpServer) {
        m_pTcpServer->OnSessionEventRemoved(this);
    }
    if (GetEvent()) {
        GetEvent()->AddPipTask([this]()
        {
            delete this;
        });
    } else {
        delete this;
    }
    return NoneError;
}

Uint32 CMkTcpSession::Check(const CMkTime& NowTime)
{
    if (NowTime >= GetActiveTime() + GetTcpActiveMs() * 1000) {
        MkWarningLog("this:%p is not active %d sec ago close it\n", this, GetTcpActiveMs() / 1000);
        Close();
    }
    return NoneError;
}

CMkTcpSession::~CMkTcpSession()
{
    m_pTcpServer = nullptr;
}